//注册页
class registered {
  constructor() {
    //绑定注册点击事件
    this.$(".btn-danger").addEventListener("click", this.checkRegister);
  }
  checkRegister = () => {
    //获取input框
    let form = document.forms[0].elements;
    // console.log(form);
    //获取账号,用户昵称,密码,确认密码框
    //账号,账号正则验证
    let account = form.account.value.trim();
    let accregular = /^[a-z0-9]\w{4,11}$/;
    //昵称
    let nickname = form.nickname.value.trim();
    //密码,密码正则验证
    let password = form.password.value.trim();
    let passregular = /\w{6,12}/;
    //确认密码
    let Confirmpass = form.Confirmpass.value.trim();
    // console.log(account, nickname, password, Confirmpass);
    //非空验证
    if (!account || !password || !nickname || !Confirmpass) {
      layer.msg("账号密码不能为空...");
      return;
    }
    //验证两次密码是否一致
    if (password != Confirmpass) {
      layer.msg("两次密码不一致...");
      return;
    }
    //判断账号是否符合要求
    if (!accregular.test(account)) {
      layer.alert("账号格式不对,正确格式为长度为4-11位并以字母和数字开头...");
      return;
    }
    //判断密码是否符合要求
    if (!passregular.test(password)) {
      layer.alert("密码格式不对哦,密码长度为6-12位...");
      return;
    }
    //设置编码方式和请求头
    // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
    axios.defaults.headers.post["Content-Type"] =
      "application/x-www-form-urlencoded";
    //设置ajax所需数据
    let str = `username=${account}&password=${password}&rpassword=${Confirmpass}&nickname=${nickname}`;
    //发送ajax
    axios
      .post("http://localhost:8888/users/register", str)
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });
  };
  //封装获取节点
  $(e) {
    let res = document.querySelectorAll(e);
    return res.length == 1 ? res[0] : res;
  }
}
new registered();
